package com.longxiao.manager.web;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.longxiao.entity.SysConfig;
import com.longxiao.service.SysConfigService;
import com.longxiao.vo.DwzResult;
import com.longxiao.vo.PageVo;

@Controller
@RequestMapping("config")
public class ConfigController extends BaseController {

	private String pageList = "/config/list";
	@Resource
	private SysConfigService sysConfigService;
	
	@RequestMapping(value = "toAdd",method={RequestMethod.GET})
	public ModelAndView toAdd(HttpServletRequest request) {
		
		return new ModelAndView("/config/add");
	}
	
	@RequestMapping(value = "addConfig",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public DwzResult<Void> addConfig(
			@RequestParam(value = "key")String key,
			@RequestParam(value = "value")String value,
			@RequestParam(value = "descr")String descr,HttpServletRequest request) {
		
		int count = sysConfigService.countSysConfigList(key);
		if(count > 0) {
			return DwzResult.fail("配置名称已存在");
		}
		SysConfig config = new SysConfig();
		config.setKeyword(key);
		config.setCreatedTime(new Date());
		config.setDescr(descr);
		config.setModifiedTime(new Date());
		config.setVal(value);
		if(sysConfigService.addConfig(config)) {
			return DwzResult.success();
		}
		return DwzResult.fail("操作异常");
	}
	
	@RequestMapping(value = "toUpdate",method={RequestMethod.GET})
	public ModelAndView toUpdate(Integer id,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/config/update");
		SysConfig config = sysConfigService.getById(id);
		mav.addObject("config", config);
		return mav;
	}
	
	@RequestMapping(value = "updateConfig",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public DwzResult<Void> updateConfig(
			@RequestParam(value = "id")Integer id,
			@RequestParam(value = "key")String key,
			@RequestParam(value = "value")String value,
			@RequestParam(value = "descr")String descr,HttpServletRequest request) {
		
		SysConfig config = sysConfigService.getById(id);
		
		if(config == null) {
			return DwzResult.fail("配置不存在");
		}
		config.setKeyword(key);
		config.setDescr(descr);
		config.setModifiedTime(new Date());
		config.setVal(value);
		if(sysConfigService.updateConfig(config)) {
			return DwzResult.success();
		}
		return DwzResult.fail("操作异常");
	}
	
	@RequestMapping(value = "getList",method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView getList(@RequestParam(value = "key",required = false)String key,
			@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
			HttpServletRequest request) {
		logger.info("查询配置信息：{}",key);
		ModelAndView mav = new ModelAndView(pageList);
		try {
			int count = sysConfigService.countSysConfigList(key);
			PageVo<SysConfig> pageInfo = new PageVo<SysConfig>(pageNum, count);
			List<SysConfig> configList = sysConfigService.getSysConfigList(key, pageInfo.getOffset(), pageInfo.getRows());
			pageInfo.setData(configList);
			mav.addObject("totalCount", count);
			mav.addObject("sysList", configList);
			mav.addObject("currentPage", pageNum);
			mav.addObject("page", pageInfo);
		} catch(Exception e) {
			logger.error("查询配置信息失败：" + e.getMessage(),e);
		}
		initParam(request,mav);
		return mav;
	}
}
